﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Globalization;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web.Security;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.IO;
using SQLExecute;


public partial class profile_edu : System.Web.UI.Page
{
    MajorSubject m = new MajorSubject();
    school s = new school();
    DataSet ds;
    Guid id = new Guid();
    Profiles p = new Profiles();
    Common c = new Common();
    Recruitment_Information r = new Recruitment_Information();
    Skills_And_Qualifications skills = new Skills_And_Qualifications();
    Working_Experience w = new Working_Experience();
    Interview_Evaluation interview = new Interview_Evaluation();
    ReportDocument rptDoc = new ReportDocument();
    public string strMessage;
    bool bUpload = false;
    private static string strImageProfile = string.Empty;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Params["id"] == null && Session["username"] != null && Session["fullname"] != null)
            {
                Response.Redirect("list_profiles.aspx");
            }
            else 
            
                if (Request.Params["id"] != null && Session["username"] != null && Session["fullname"] != null)
            
                {
                    AddOnClick();
                    InitCombo();
                    Show_Profile_Info();
                    lUsername.Text = Session["username"].ToString();
                    popupEdit.Hide();
                }
              
                else  Response.Redirect("../login/login.aspx");
                 

        }        
     }



   
    private void Show_Profile_Info()
    {
        
        id = new Guid(Request.QueryString[0]);
        Profiles p = new Profiles();
        p.ID = id;
        ds = p.Get_Profile();
    
      if (ds.Tables[0].Rows.Count > 0)
        {
            Get_Basic_Info();
            Get_Personal_Info();
            Get_Skills_And_Qualification();
            Get_Working_Experience();
            Get_Interview_Evaluation();
            Get_Recruitment_Information();
        }

    }
    private void Get_Basic_Info()
    {
        txtPosition.Text = ds.Tables[0].Rows[0]["pos"].ToString();

        if (ds.Tables[0].Rows[0]["availabledate"].ToString() != "")

            txtAvailableDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["availabledate"].ToString()).ToString("dd/MM/yyyy");
        
        if (ds.Tables[0].Rows[0]["expectedsalary"].ToString()!="" && ds.Tables[0].Rows[0]["expectedsalary"].ToString()!="0")
            txtExpectedSalary.Text = ds.Tables[0].Rows[0]["expectedsalary"].ToString();
       // if (cbKnownUs.SelectedValue != "-1") 
            cbKnownUs.SelectedValue = ds.Tables[0].Rows[0]["known_from_id"].ToString();
        txtCarreerObjectives.Text = ds.Tables[0].Rows[0]["carreerobjectives"].ToString();
        if (ds.Tables[0].Rows[0]["currency_salary"].ToString() == "USD")
            radioUSD.Checked = true;
        else radioVND.Checked = true;

    }
    private void Get_Personal_Info()
    {
        txtFullName.Text = ds.Tables[0].Rows[0]["fullname"].ToString();
        if (ds.Tables[0].Rows[0]["birth"].ToString() != "")
        {
            //txtBirth.Text =DateTime.Parse(ds.Tables[0].Rows[0]["birth"].ToString()).ToShortDateString();
            txtBirth.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["birth"].ToString()).ToString("dd/MM/yyyy");
        }
        txtIDCard.Text = ds.Tables[0].Rows[0]["id_card"].ToString();
        if (ds.Tables[0].Rows[0]["dateidcard"].ToString() != "")
        {
            txtDateIDCard.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["dateidcard"].ToString()).ToString("dd/MM/yyyy");
        }
        txtPlaceIssueIDCard.Text = ds.Tables[0].Rows[0]["placeissueidcard"].ToString();

        if (ds.Tables[0].Rows[0]["sex"].ToString() == "Female")
            chkFemale.Checked = true;
        else chkMale.Checked = true;

        if (ds.Tables[0].Rows[0]["height"].ToString() != "" && ds.Tables[0].Rows[0]["height"].ToString()!="0")
            txtHeight.Text = ds.Tables[0].Rows[0]["height"].ToString();
        if (ds.Tables[0].Rows[0]["weight"].ToString() != "" && ds.Tables[0].Rows[0]["weight"].ToString()!="0")
            txtWeight.Text = ds.Tables[0].Rows[0]["weight"].ToString();
        txtPhone.Text = ds.Tables[0].Rows[0]["phone"].ToString();
        txtMail.Text = ds.Tables[0].Rows[0]["mail"].ToString();

        if (ds.Tables[0].Rows[0]["married"].ToString() == "Single")
            chkSingle.Checked = true;

        if (ds.Tables[0].Rows[0]["married"].ToString() == "Married")
            chkMarried.Checked = true;

        else if (ds.Tables[0].Rows[0]["number_of_childrens"].ToString() != "")
        {
           
            txtNumberChildren.Text = ds.Tables[0].Rows[0]["number_of_childrens"].ToString();
        }
        else if (ds.Tables[0].Rows[0]["married_status_others"].ToString() != "")
        {
            txtOtherMartialStatus.Text = ds.Tables[0].Rows[0]["married_status_others"].ToString();
           
        }

        txtPermanentAddress.Text = ds.Tables[0].Rows[0]["permanent_address"].ToString();
     

        txtCurrentAddress.Text = ds.Tables[0].Rows[0]["current_address"].ToString();
        if (ds.Tables[0].Rows[0]["relocate"].ToString() == "Yes")
            chkRelocateYes.Checked = true;
        if (ds.Tables[0].Rows[0]["relocate"].ToString() == "No")
            chkRelocateNo.Checked = true;
        if (ds.Tables[0].Rows[0]["relocate"].ToString() == "Not sure")
            chkRelocateNotSure.Checked = true;

        txtAptitude.Text = ds.Tables[0].Rows[0]["aptitude"].ToString();
        if (ds.Tables[0].Rows[0]["image_URL"].ToString() != "")
        {
            imgProfile.ImageUrl=  ds.Tables[0].Rows[0]["image_URL"].ToString();
            strImageProfile = ds.Tables[0].Rows[0]["image_URL"].ToString();
        }


    }
    private string[] Split_Study_Period_Time(string s)
    {
        string[] s1 = new string[2];

        if (s != "")
        {
            s1[0] = s.Substring(0, 4);
            s1[1] = s.Substring(s.Length - 4, 4);
        }


        return s1;
    }
    private void Get_Skills_And_Qualification()
    {

        if (ds.Tables[0].Rows[0]["degree_type"].ToString() == "Master")
            chkMaster.Checked = true;
        if (ds.Tables[0].Rows[0]["degree_type"].ToString() == "Bachelor")
            chkBachelor.Checked = true;
        if (ds.Tables[0].Rows[0]["degree_type"].ToString() == "Diploma")
            chkDiploma.Checked = true;
        if (ds.Tables[0].Rows[0]["degree_type"].ToString() == "Associate")
            chkAssociate.Checked = true;
        if (ds.Tables[0].Rows[0]["degree_type"].ToString() == "Other degree")
            chkOtherDeg.Checked = true;
        //,s.info_degree1,s.info_degree2,s.info_degree3,s.info_degree4
        cbGraduationDegree1.SelectedValue = ds.Tables[0].Rows[0]["info_degree1"].ToString();

        if (ds.Tables[0].Rows[0]["info_degree2"].ToString() != "")
            cbGraduationDegree2.SelectedValue = ds.Tables[0].Rows[0]["info_degree2"].ToString();
        if (ds.Tables[0].Rows[0]["info_degree3"].ToString() != "")
            cbGraduationDegree3.SelectedValue = ds.Tables[0].Rows[0]["info_degree3"].ToString();
        if (ds.Tables[0].Rows[0]["info_degree4"].ToString() != "")
            cbGraduationDegree4.SelectedValue = ds.Tables[0].Rows[0]["info_degree4"].ToString();

        
        cbMajor1.SelectedValue = ds.Tables[0].Rows[0]["major1"].ToString();
        if (ds.Tables[0].Rows[0]["major2"].ToString() != "")
            cbMajor2.SelectedValue = ds.Tables[0].Rows[0]["major2"].ToString();
        if (ds.Tables[0].Rows[0]["major3"].ToString() != "")
            cbMajor3.SelectedValue = ds.Tables[0].Rows[0]["major3"].ToString();
        if (ds.Tables[0].Rows[0]["major4"].ToString() != "")
            cbMajor4.SelectedValue = ds.Tables[0].Rows[0]["major4"].ToString();



        cbSchool1.SelectedValue = ds.Tables[0].Rows[0]["school1"].ToString();
        if (ds.Tables[0].Rows[0]["school2"].ToString() != "")
            cbSchool2.SelectedValue = ds.Tables[0].Rows[0]["school2"].ToString();
        if (ds.Tables[0].Rows[0]["school3"].ToString() != "")
            cbSchool3.SelectedValue = ds.Tables[0].Rows[0]["school3"].ToString();
        if (ds.Tables[0].Rows[0]["school4"].ToString() != "")
            cbSchool4.SelectedValue = ds.Tables[0].Rows[0]["school4"].ToString();



        cbTypeOfTraining1.SelectedValue = ds.Tables[0].Rows[0]["type_of_training1"].ToString();
        if (ds.Tables[0].Rows[0]["type_of_training2"].ToString() != "")
            cbTypeOfTraining2.SelectedValue = ds.Tables[0].Rows[0]["type_of_training2"].ToString();
        if (ds.Tables[0].Rows[0]["type_of_training3"].ToString() != "")
            cbTypeOfTraining3.SelectedValue = ds.Tables[0].Rows[0]["type_of_training3"].ToString();
        if (ds.Tables[0].Rows[0]["type_of_training4"].ToString() != "")
            cbTypeOfTraining4.SelectedValue = ds.Tables[0].Rows[0]["type_of_training4"].ToString();

        if (ds.Tables[0].Rows[0]["english_skills"].ToString() == "Basic")
            chkBasic.Checked = true;
        if (ds.Tables[0].Rows[0]["english_skills"].ToString() == "Intermediate")
            chkIntermediate.Checked = true;
        if (ds.Tables[0].Rows[0]["english_skills"].ToString() == "Advance")
            chkAdvance.Checked = true;

        txtOtherLanguage.Text = ds.Tables[0].Rows[0]["other_language"].ToString();

        if (ds.Tables[0].Rows[0]["computer_skills"].ToString() == "Basic")
            chkComputerSkillBasic.Checked = true;
        if (ds.Tables[0].Rows[0]["computer_skills"].ToString() == "Intermediate")
            chkComputerSkillIntermediate.Checked = true;
        if (ds.Tables[0].Rows[0]["computer_skills"].ToString() == "Advance")
            chkComputerSkillAdvance.Checked = true;

        txtOtherSkills.Text = ds.Tables[0].Rows[0]["other_skills"].ToString();        
        txtStudyPeriod1.Text=ds.Tables[0].Rows[0]["info_degree_date1"].ToString();
        txtStudyPeriod2.Text=ds.Tables[0].Rows[0]["info_degree_date2"].ToString();
        txtStudyPeriod3.Text=ds.Tables[0].Rows[0]["info_degree_date3"].ToString();
        txtStudyPeriod4.Text=ds.Tables[0].Rows[0]["info_degree_date4"].ToString();
        
        
    }
    private string[] Split_Number_Char(string s)
    {
        string[] s1 = new string[3];
        int n1 = 0, n2 = 0;

        if (s != "")
        {
            n1 = s.IndexOf("-", 0);
            if (n1 > 0)
            {
                n1 = s.IndexOf("-", 0);
                s1[0] = s.Substring(n1 + 1, 4);
                n2 = s.IndexOf("o", n1 + 1);
                s1[1] = s.Substring(s.Length - 4, 4);
                s1[2] = s.Substring(0, n1);
            }
        }

        return s1;
    }
    private void Get_Working_Experience()
    {
        txtEmployer1.Text = ds.Tables[0].Rows[0]["employer1"].ToString();
        if (ds.Tables[0].Rows[0]["employer2"].ToString() != "")
            txtEmployer2.Text = ds.Tables[0].Rows[0]["employer2"].ToString();
        if (ds.Tables[0].Rows[0]["employer3"].ToString() != "")
            txtEmployer3.Text = ds.Tables[0].Rows[0]["employer3"].ToString();
        if (ds.Tables[0].Rows[0]["sal1"].ToString() != "" && ds.Tables[0].Rows[0]["sal1"].ToString()!="0")
            
                txtSalary1.Text = ds.Tables[0].Rows[0]["sal1"].ToString();

        if (ds.Tables[0].Rows[0]["sal2"].ToString() != "" && ds.Tables[0].Rows[0]["sal2"].ToString()!="0")
           
            txtSalary2.Text = ds.Tables[0].Rows[0]["sal2"].ToString();
        if (ds.Tables[0].Rows[0]["sal3"].ToString() != "" && ds.Tables[0].Rows[0]["sal3"].ToString()!="0")
            txtSalary3.Text = ds.Tables[0].Rows[0]["sal3"].ToString();

        if (ds.Tables[0].Rows[0]["type_sal"].ToString() != "")
        {

            if (ds.Tables[0].Rows[0]["type_sal"].ToString() == "VND")            
                beforeSalaryVND.Checked = true;            
            else if (ds.Tables[0].Rows[0]["type_sal"].ToString() == "USD")
                beforeSalaryUSD.Checked = true;
        }
        txtSupervisor1.Text = ds.Tables[0].Rows[0]["supervisor1"].ToString();
        if (ds.Tables[0].Rows[0]["supervisor2"].ToString() != "")
            txtSupervisor2.Text = ds.Tables[0].Rows[0]["supervisor2"].ToString();
        if (ds.Tables[0].Rows[0]["supervisor3"].ToString() != "")
            txtSupervisor3.Text = ds.Tables[0].Rows[0]["supervisor3"].ToString();


        if (ds.Tables[0].Rows[0]["pos1"].ToString() != "")        
            txtPosition1.Text = ds.Tables[0].Rows[0]["pos1"].ToString();
        if (ds.Tables[0].Rows[0]["pos2"].ToString() != "")
            txtPosition2.Text = ds.Tables[0].Rows[0]["pos2"].ToString();
        if (ds.Tables[0].Rows[0]["pos3"].ToString() != "")
            txtPosition3.Text = ds.Tables[0].Rows[0]["pos3"].ToString();
    }
    private void Get_Interview_Evaluation()
    {
        txtInterView1.Text = ds.Tables[0].Rows[0]["interview1"].ToString();
        txtInterViewPos1.Text = ds.Tables[0].Rows[0]["interviewpos1"].ToString();
        txtInterViewDept1.Text = ds.Tables[0].Rows[0]["interviewdep1"].ToString();
        txtComment1.Text = ds.Tables[0].Rows[0]["comment1"].ToString();

        
        txtEnglish.Text = ds.Tables[0].Rows[0]["english_result"].ToString();
        txtWord.Text = ds.Tables[0].Rows[0]["word_result"].ToString();
        txtExcel.Text = ds.Tables[0].Rows[0]["excel_result"].ToString();

        if (ds.Tables[0].Rows[0]["interview_conclusion"].ToString() == "Accepted")
            chkAccepted.Checked = true;
        if (ds.Tables[0].Rows[0]["interview_conclusion"].ToString() == "Considering")
            chkKIV.Checked = true;
        if (ds.Tables[0].Rows[0]["interview_conclusion"].ToString() == "Rejected")
            chkRejected.Checked = true;


        if (ds.Tables[0].Rows[0]["interview_conclusion1"].ToString() == "Accepted")
            chkAccepted1.Checked = true;
        if (ds.Tables[0].Rows[0]["interview_conclusion1"].ToString() == "Considering")
            chkKIV1.Checked = true;
        if (ds.Tables[0].Rows[0]["interview_conclusion1"].ToString() == "Rejected")
            chkRejected1.Checked = true;

        txtInterView2.Text = ds.Tables[0].Rows[0]["interview2"].ToString();
        txtInterViewPos2.Text = ds.Tables[0].Rows[0]["interviewpos2"].ToString();
        txtInterViewDept2.Text = ds.Tables[0].Rows[0]["interviewdep2"].ToString();
        txtComment2.Text = ds.Tables[0].Rows[0]["comment2"].ToString();

        txtFinalDecision.Text = ds.Tables[0].Rows[0]["final_decision"].ToString();
        txtInterViewPos3.Text = ds.Tables[0].Rows[0]["interviewpos3"].ToString();
        txtInterViewDept3.Text = ds.Tables[0].Rows[0]["interviewdep3"].ToString();
        txtComment3.Text = ds.Tables[0].Rows[0]["comment3"].ToString();


    }
    private void Get_Recruitment_Information()
    {


        if (ds.Tables[0].Rows[0]["recruitment_conclusion"].ToString() == "Accepted")
            chkFinalAccept.Checked = true;
        if (ds.Tables[0].Rows[0]["recruitment_conclusion"].ToString() == "Considering")
            chkFinalConsiderMore.Checked = true;
        if (ds.Tables[0].Rows[0]["recruitment_conclusion"].ToString() == "Rejected")
            chkFinalRejected.Checked = true;
        if (ds.Tables[0].Rows[0]["company_id"].ToString() != "")
            cbCompany.Text = ds.Tables[0].Rows[0]["company_id"].ToString();

        txtPositionOffer.Text = ds.Tables[0].Rows[0]["position_offer"].ToString();

        txtRecruitmentDept.Text = ds.Tables[0].Rows[0]["dept"].ToString();
        if (ds.Tables[0].Rows[0]["salary"].ToString()!=""&&ds.Tables[0].Rows[0]["salary"].ToString()!="0")
            txtRecruitmentSalary.Text = ds.Tables[0].Rows[0]["salary"].ToString();
        if (ds.Tables[0].Rows[0]["starting_date"].ToString() != "")
            txtRecruitmentStartingDate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["starting_date"].ToString()).ToString("dd/MM/yyyy");       
        txtNote.Text = ds.Tables[0].Rows[0]["note"].ToString();


    }
    private void Get_Known_Us()
    {
        Common c = new Common();
        cbKnownUs.DataSource = c.Get_Known_Us();
        cbKnownUs.DataTextField = "known_from";
        cbKnownUs.DataValueField = "id";
        cbKnownUs.DataBind();
        cbKnownUs.SelectedIndex = 0;
    }

    private void AddOnClick()
    {
        beforeSalaryUSD.Attributes.Add("OnClick", "beforeSalaryUSDCheck();");
        beforeSalaryVND.Attributes.Add("OnClick", "beforeSalaryVNDCheck();");

        radioUSD.Attributes.Add("OnClick", "checkSalaryUSD();");
        radioVND.Attributes.Add("OnClick", "checkSalaryVND();");
        chkMale.Attributes.Add("OnClick", "checkMale();");
        chkFemale.Attributes.Add("OnClick", "checkFemale();");

        chkSingle.Attributes.Add("OnClick", "checkSingle();");
        chkMarried.Attributes.Add("OnClick", "checkMarried();");
        //chkNumberChildren.Attributes.Add("OnClick", "checkNumberOfChildrens();");
        //chkOthers.Attributes.Add("OnClick", "checkOthers();");

        chkRelocateYes.Attributes.Add("OnClick", "checkRelocateYes();");
        chkRelocateNo.Attributes.Add("OnClick", "checkRelocateNo();");
        chkRelocateNotSure.Attributes.Add("OnClick", "checkRelocateNotSure();");

        chkMaster.Attributes.Add("OnClick", "checkMaster();");
        chkBachelor.Attributes.Add("OnClick", "checkBachelor();");
        chkDiploma.Attributes.Add("OnClick", "checkDiploma();");
        chkAssociate.Attributes.Add("OnClick", "checkAssociate();");
        chkOtherDeg.Attributes.Add("OnClick", "checkOtherSkills();");

        chkBasic.Attributes.Add("OnClick", "checkBasic();");
        chkIntermediate.Attributes.Add("OnClick", "checkIntermediate();");
        chkAdvance.Attributes.Add("OnClick", "checkAdvance();");

        chkComputerSkillBasic.Attributes.Add("OnClick", "checkComputerSkillBasic();");
        chkComputerSkillIntermediate.Attributes.Add("OnClick", "checkComputerSkillIntermediate();");
        chkComputerSkillAdvance.Attributes.Add("OnClick", "checkComputerSkillAdvance();");

        chkRejected.Attributes.Add("OnClick", "checkRejected();");
        chkKIV.Attributes.Add("OnClick", "checkKIV();");
        chkAccepted.Attributes.Add("OnClick", "checkAccepted();");


        chkRejected1.Attributes.Add("OnClick", "checkRejected1();");
        chkKIV1.Attributes.Add("OnClick", "checkKIV1();");
        chkAccepted1.Attributes.Add("OnClick", "checkAccepted1();");

        chkFinalAccept.Attributes.Add("OnClick", "checkFinalAccept();");
        chkFinalRejected.Attributes.Add("OnClick", "checkFinalRejected();");
        chkFinalConsiderMore.Attributes.Add("OnClick", "checkFinalConsiderMore();");
    }
    private void InitCombo()
    {

       
        c.Init_KnownUs(this.cbKnownUs);
      
        c.Init_Graduation_Degree(this.cbGraduationDegree1, this.cbGraduationDegree2, this.cbGraduationDegree3, this.cbGraduationDegree4);
        m.Init_Major_Subject2(cbMajor1, cbMajor2, cbMajor3, cbMajor4);
        s.Init_Schools(this.cbSchool1, this.cbSchool2, this.cbSchool3, this.cbSchool4);
        c.InitTypeOfTraining(this.cbTypeOfTraining1, this.cbTypeOfTraining2, this.cbTypeOfTraining3, this.cbTypeOfTraining4);
        r.Init_Company(cbCompany);

    }
       
   
    private static void ResetControl(Page mypage)
    {

        Control myForm = mypage.FindControl("Form1");
        foreach (Control ctrl in myForm.Controls)
        {
            //Clears TextBox
            if (ctrl is System.Web.UI.WebControls.TextBox)
            {
                (ctrl as TextBox).Text = string.Empty;
            }
            if (ctrl is System.Web.UI.WebControls.RadioButton)
            {
                (ctrl as RadioButton).Checked = false;
            }
            if (ctrl is System.Web.UI.WebControls.CheckBox)
            {
                (ctrl as CheckBox).Checked = false;
            }
            if (ctrl is System.Web.UI.WebControls.DropDownList)
            {
                (ctrl as DropDownList).SelectedIndex = 0;
            }
        }



    }
    protected void btnView_Click(object sender, EventArgs e)
    {
        //  Insert_Profile_Info();


    }
    public static void ResetControl_archive(Page mypage)
    {
        Control myForm = mypage.FindControl("Form1");
        foreach (Control ctrl in myForm.Controls)
        {
            //Clears TextBox
            if (ctrl is System.Web.UI.WebControls.TextBox)
            {
                (ctrl as TextBox).Text = string.Empty;
            }

            //Clears DropDown Selection
            if (ctrl is System.Web.UI.WebControls.DropDownList)
            {
                (ctrl as DropDownList).ClearSelection();
            }

            //Clears ListBox Selection
            if (ctrl is System.Web.UI.WebControls.ListBox)
            {
                (ctrl as ListBox).ClearSelection();
            }

            //Clears CheckBox Selection
            if (ctrl is System.Web.UI.WebControls.CheckBox)
            {
                (ctrl as CheckBox).Checked = false;
            }

            //Clears RadioButton Selection

            if (ctrl is System.Web.UI.WebControls.RadioButtonList)
            {
                (ctrl as RadioButtonList).ClearSelection();
            }

            //Clears CheckBox Selection
            if (ctrl is System.Web.UI.WebControls.CheckBoxList)
            {
                (ctrl as CheckBoxList).ClearSelection();
            }
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("list_profiles.aspx");
    }
  
    /*----------------------- UPdate---------------------*/
    private void Set_Basic_Info(Profiles p)
    {    
        string strID = Request.QueryString["id"].ToString();
        id = new Guid(strID);   
        p.ID = id;    
        p.PositionApply = txtPosition.Text;
        if (txtAvailableDate.Text != "")
        {
            string s = Common.Convert_DDMMYYYY_To_MMDDYYY(txtAvailableDate.Text);
            p.AvailableDate = DateTime.Parse(s);
        }
        else p.AvailableDate = DateTime.Parse("01/01/1900");


        if (txtExpectedSalary.Text != "")
        {
            string s;
            s = txtExpectedSalary.Text.Trim();
            s = s.Replace(",", "");
            p.ExpectedSalary = Convert.ToInt32(s);
        }
        if (radioUSD.Checked)
            p.CurrencySalary = radioUSD.Text;
        if (radioVND.Checked)
            p.CurrencySalary = radioVND.Text;
        if (cbKnownUs.SelectedValue != "-1") 
            p.KnownUsFrom = Convert.ToInt16(cbKnownUs.SelectedValue);
        p.CarreerObjectives = txtCarreerObjectives.Text;

    }

    private void Set_Personal_Info(Profiles p)
    {
        p.Fullname = txtFullName.Text;
        if (txtBirth.Text != "")
        {
            string s = Common.Convert_DDMMYYYY_To_MMDDYYY(txtBirth.Text);
            p.Birth = DateTime.Parse(s);
        }
        else p.Birth = DateTime.Parse("01/01/1900");
           // p.Birth = DBNull.Value;



        if (chkMale.Checked)
            p.Sex = "Male";
        else if (chkFemale.Checked) p.Sex = "Female";
        if (txtHeight.Text.Trim() != "")
        {
            if (Convert.ToInt16(txtHeight.Text)>0)
                p.Height = Convert.ToInt16(txtHeight.Text);
        }
        if (txtWeight.Text.Trim() != "")
        {
            
            if (Convert.ToInt16(txtWeight.Text)>0)
                p.Weight = Convert.ToInt16(txtWeight.Text);
        }


        if (chkMarried.Checked)
            p.Married = "Married";
        if (chkSingle.Checked)
            p.Married = "Single";

        if (txtOtherMartialStatus.Text != "") 
            p.OthersMartialStatus = txtOtherMartialStatus.Text;
        if (txtNumberChildren.Text != "")
            p.NumberOfChildrens = txtNumberChildren.Text;


        p.Phone = txtPhone.Text;
        p.Mail = txtMail.Text;
        p.IDCard = txtIDCard.Text;

       

        if (txtDateIDCard.Text != "")
        {
            string s = Common.Convert_DDMMYYYY_To_MMDDYYY(txtDateIDCard.Text);
            p.DateIssueIDCard = DateTime.Parse(s);
        }
          else p.DateIssueIDCard=DateTime.Parse("01/01/1900");
        p.PlaceIssueIDCard = txtPlaceIssueIDCard.Text;
        p.PermanentAddress = txtPermanentAddress.Text;
        p.CurrentAddress = txtCurrentAddress.Text;

        if (chkRelocateNo.Checked)
            p.Relocate = chkRelocateNo.Text;
        if (chkRelocateYes.Checked)
            p.Relocate = chkRelocateYes.Text;
        if (chkRelocateNotSure.Checked)
            p.Relocate = chkRelocateNotSure.Text;

        p.Aptitude = txtAptitude.Text;

        if (strImageProfile != string.Empty)
        {
            p.ImageURL =strImageProfile;
        }

    }
    private void Set_Skills_And_Qualifications()
    {
        if (cbMajor1.SelectedValue!="CHOOSE ONE")
            skills.Major1 =Convert.ToInt16(cbMajor1.SelectedValue);

        if (cbMajor2.SelectedValue != "CHOOSE ONE")
            skills.Major2 =Convert.ToInt16(cbMajor2.SelectedValue);
        if (cbMajor3.SelectedValue != "CHOOSE ONE")
            skills.Major3 = Convert.ToInt16(cbMajor3.SelectedValue);
        if (cbMajor4.SelectedValue != "CHOOSE ONE")
            skills.Major4 =Convert.ToInt16(cbMajor4.SelectedValue);

        if (cbSchool1.SelectedIndex > 0)
            skills.School1 = cbSchool1.SelectedValue;
        if (cbSchool2.SelectedIndex > 0)
            skills.School2 = cbSchool2.SelectedValue;
        if (cbSchool3.SelectedIndex > 0)
            skills.School3 = cbSchool3.SelectedValue;
        if (cbSchool3.SelectedIndex > 0)
            skills.School4 = cbSchool4.SelectedValue;

        if (cbTypeOfTraining1.SelectedIndex > 0)
            skills.Typetraining1 = cbTypeOfTraining1.SelectedValue;
        if (cbTypeOfTraining2.SelectedIndex > 0)
            skills.Typetraining2 = cbTypeOfTraining2.SelectedValue;
        if (cbTypeOfTraining3.SelectedIndex > 0)
            skills.Typetraining3 = cbTypeOfTraining3.SelectedValue;
        if (cbTypeOfTraining4.SelectedIndex > 0)
            skills.Typetraining4 = cbTypeOfTraining4.SelectedValue;

        if (cbGraduationDegree1.SelectedIndex > 0)
            skills.InfoDegree1 = cbGraduationDegree1.SelectedValue;
        if (cbGraduationDegree2.SelectedIndex > 0)
            skills.InfoDegree2 = cbGraduationDegree2.SelectedValue;
        if (cbGraduationDegree3.SelectedIndex > 0)
            skills.InfoDegree3 = cbGraduationDegree3.SelectedValue;
        if (cbGraduationDegree4.SelectedIndex > 0)
            skills.InfoDegree4 = cbGraduationDegree4.SelectedValue;

        skills.Info_Degree_Date1 = txtStudyPeriod1.Text;
        skills.Info_Degree_Date2 = txtStudyPeriod2.Text;
        skills.Info_Degree_Date3 = txtStudyPeriod3.Text;
        skills.Info_Degree_Date4 = txtStudyPeriod4.Text;

        if (chkBasic.Checked)
            skills.EnglishSkills = chkBasic.Text;
        if (chkIntermediate.Checked)
            skills.EnglishSkills = chkIntermediate.Text;
        if (chkAdvance.Checked)
            skills.EnglishSkills = chkAdvance.Text;
        
        if (chkComputerSkillBasic.Checked)
            skills.ComputerSkills = chkComputerSkillBasic.Text;
        if (chkComputerSkillIntermediate.Checked)
            skills.ComputerSkills = chkComputerSkillIntermediate.Text;
        if (chkComputerSkillAdvance.Checked)
            skills.ComputerSkills = chkComputerSkillAdvance.Text;

        skills.OtherLanguage = txtOtherLanguage.Text;
        skills.OtherSkills = txtOtherSkills.Text;

        if (chkMaster.Checked)
            skills.DegreeType = "Master";
        if (chkBachelor.Checked)
            skills.DegreeType = "Bachelor";
        if (chkDiploma.Checked)
            skills.DegreeType = "Diploma";

        if (chkAssociate.Checked)
            skills.DegreeType = "Associate";
        if (chkOtherDeg.Checked)
            skills.DegreeType = "Other degree";

        if (beforeSalaryUSD.Checked)
            w.TypeSalary = beforeSalaryUSD.Text;
        if (beforeSalaryVND.Checked)
            w.TypeSalary = beforeSalaryVND.Text;
        
    }
    private void Set_Working_Experience(Profiles p)
    {
        w.Employer1 = txtEmployer1.Text;
        w.Employer2 = txtEmployer2.Text;
        w.Employer3 = txtEmployer3.Text;

        if (txtPosition1.Text != "")
            w.Pos1 = txtPosition1.Text ;
        
        if (txtPosition2.Text != "")
           w.Pos2 = txtPosition2.Text;
        
        if (txtPosition3.Text != "")
            w.Pos3 = txtPosition3.Text;
        



        txtSalary1.Text = txtSalary1.Text.Trim();
        txtSalary1.Text = txtSalary1.Text.Replace(",", "");
        txtSalary2.Text = txtSalary2.Text.Trim();
        txtSalary2.Text = txtSalary2.Text.Replace(",", "");
        txtSalary3.Text = txtSalary3.Text.Trim();
        txtSalary3.Text = txtSalary3.Text.Replace(",", "");
        if (txtSalary1.Text != "")
            w.Salary1 = Convert.ToInt32(txtSalary1.Text);


        if (txtSalary2.Text != "")
            w.Salary2 = Convert.ToInt32(txtSalary2.Text);
        if (txtSalary3.Text != "")
            w.Salary3 = Convert.ToInt32(txtSalary3.Text);


        if (beforeSalaryUSD.Checked)
            w.TypeSalary = beforeSalaryUSD.Text;
        if (beforeSalaryVND.Checked)
            w.TypeSalary = beforeSalaryVND.Text;

        w.Supervisor1 = txtSupervisor1.Text;
        w.Supervisor2 = txtSupervisor2.Text;
        w.Supervisor3 = txtSupervisor3.Text;

    }
    protected void Set_Interview_Evaluation()
    {
        interview.InterView1 = txtInterView1.Text;
        interview.InterViewPos1 = txtInterViewPos1.Text;
        interview.InterViewDep1 = txtInterViewDept1.Text;
        interview.Comment1 = txtComment1.Text;

        interview.EnglishResult = txtEnglish.Text;
        interview.SkillWord = txtWord.Text;
        interview.SkillExcel = txtExcel.Text;
        if (chkAccepted.Checked)
            interview.Conclusion = chkAccepted.Text;
        if (chkKIV.Checked)
            interview.Conclusion = chkKIV.Text;
        if (chkRejected.Checked)
            interview.Conclusion = chkRejected.Text;

        if (chkAccepted1.Checked)
            interview.Conclusion1 = chkAccepted1.Text;
        if (chkKIV1.Checked)
            interview.Conclusion1 = chkKIV1.Text;
        if (chkRejected1.Checked)
            interview.Conclusion1 = chkRejected1.Text;

        interview.InterView2 = txtInterView2.Text;
        interview.InterViewPos2 = txtInterViewPos2.Text;
        interview.InterViewDep2 = txtInterViewDept2.Text;
        interview.Comment2 = txtComment2.Text;


        interview.FinalDecision = txtFinalDecision.Text;
        interview.InterViewPos3 = txtInterViewPos3.Text;
        interview.InterViewDep3 = txtInterViewDept3.Text;
        interview.Comment3 = txtComment3.Text;

    }
    protected void Set_Recruitment_Info()
    {
        if (chkFinalAccept.Checked)
            r.ConclusionRecruitmentInformation = chkFinalAccept.Text;
        if (chkFinalConsiderMore.Checked)
            r.ConclusionRecruitmentInformation = chkFinalConsiderMore.Text;
        if (chkFinalRejected.Checked)
            r.ConclusionRecruitmentInformation = chkFinalRejected.Text;

        if (cbCompany.SelectedIndex > 0)
            r.Company = cbCompany.SelectedValue;

        r.PositionOffer = txtPositionOffer.Text;
        r.RecruitmentDep = txtRecruitmentDept.Text;
        if (txtRecruitmentSalary.Text != "")
        {
            r.RecruitmentSalary = Convert.ToInt32(txtRecruitmentSalary.Text);
        }
        if (txtRecruitmentStartingDate.Text != "")
        {
            string s = Common.Convert_DDMMYYYY_To_MMDDYYY(txtRecruitmentStartingDate.Text);
            r.RecruitmentStartingDateTime = DateTime.Parse(s);
        }
        else r.RecruitmentStartingDateTime = DateTime.Parse("01/01/1900");
        r.RecruitemtNote = txtNote.Text;
        r.SendMail = 0;

    }

    protected void Update()
    {
        Set_Basic_Info(p);
        p.Update_Basic_Info();
        Set_Personal_Info(p);
        p.Update_Profile();

        w._id = skills._id = interview._id = r._id = p.ID;
        Set_Skills_And_Qualifications();
        skills.Update_Skills_Qualifications();
        Set_Working_Experience(p);
        w.Update_Working_Experience();
        Set_Interview_Evaluation();
        interview.Update_InterView_Evaluation();
        Set_Recruitment_Info();
        r.Update_Recruitment_Information();

    }

    private void EmptyTextbox()
    {
        txtSchoolID.Text = "";
        txtSchoolName.Text = "";
        txtTenNganhHoc.Text = "";
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        Update();
        popupUpdate.Show();

    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        Response.Redirect("list_profiles.aspx");
    }

    protected Bitmap CropImage(Bitmap bmpSource, Point topleft, Point bottomright)
    {
       
        Bitmap bmpCroped = new Bitmap(bottomright.Y - topleft.Y, bottomright.X - topleft.X);
        Graphics g = Graphics.FromImage(bmpCroped);

        g.DrawImage(bmpSource, new Rectangle(0, 0, bmpCroped.Width, bmpCroped.Height)
                    , topleft.X, topleft.Y, bmpCroped.Width, bmpCroped.Height, GraphicsUnit.Pixel);

        g.Dispose();

        return bmpCroped;
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string strFileName = fileUpload.FileName;

        if (fileUpload.HasFile)
        {


            Bitmap originalBMP = new Bitmap(fileUpload.FileContent);
            int origWidth = originalBMP.Width;
            int origHeight = originalBMP.Height;
            int sngRatio = origWidth / origHeight;
            int thumbnailSize = 250;
            int newWidth, newHeight;

            if (originalBMP.Width > originalBMP.Height)
            {
                newWidth = thumbnailSize;
                newHeight = originalBMP.Height * thumbnailSize / originalBMP.Width;
            }
            else
            {
                newWidth = originalBMP.Width * thumbnailSize / originalBMP.Height;
                newHeight = thumbnailSize;
            }

            using (Bitmap bmp = new Bitmap(newWidth, newHeight, PixelFormat.Format32bppArgb))
            {
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.DrawImage(originalBMP, 0, 0, newWidth, newHeight);

                    Bitmap bmpCroped = new Bitmap(bmp);

                    bmpCroped = CropImage(bmp, new Point(0, 0), new Point(165, 130));
                    bmpCroped.Save(Server.MapPath("~/images_profiles/" + fileUpload.FileName),System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            GC.Collect();

            fileUpload.SaveAs(Server.MapPath("~//images_profiles//" + fileUpload.FileName));

            imgProfile.ImageUrl = "~//images_profiles//" + fileUpload.FileName;
            strImageProfile = "~/images_profiles/" + fileUpload.FileName;
            bUpload = true;
        }
        

    }
    protected void lnkLogout_Click(object sender, EventArgs e)
    {
        FormsAuthentication.SignOut();
       
        Session.Clear();
        Response.Redirect("../login/login.aspx");
    }
    protected void LoadImage(DataRow r, string strImageField, string strPath)
    {
        try
        {
            FileStream f = new FileStream(strPath, FileMode.Open, FileAccess.Read);
            byte[] img = new byte[f.Length];
            f.Read(img, 0, Convert.ToInt32(f.Length));
            f.Close();
            r[strImageField] = img;
        }

        catch (Exception ex)
        {
            Response.Write("<font color=red>" + ex.Message + "</font>");
        }

    }
    protected void Export2PDF()
    {
        if (Request.Params["id"] != null)
        {

           
            string strID = Request.QueryString["id"].ToString();
            Guid id = new Guid(strID);            
            Utility ex = new Utility();
            //ex.Export2PDF2(id);            
            //ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/profile.pdf');popup.focus();", true);
            string strFullName = ex.Export2PDF2(id);
            ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", "var popup=window.open('../export/" + strFullName + "');popup.focus();", true);

        }


    }
    protected void btnExport_Click(object sender, EventArgs e)
    {

        //update
        Update();
        //export
        Export2PDF();
      }



    protected void imgAddSchool1_Click(object sender, ImageClickEventArgs e)
    {

        popupEdit.Show();
    }

    protected void imgAddMajor1_Click(object sender, ImageClickEventArgs e)
    {

        popupNganhHoc.Show();
    }

    //add new truong hoc
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sql = string.Empty;

        if (txtSchoolID.Text != "" && txtSchoolName.Text != "")
        {

            s.SchoolID = txtSchoolID.Text.Trim();
            s.SchoolName = txtSchoolName.Text.Trim();

            if (!s.School_Exists())
            {

                //s.Insert_School();
                sql = "insert into tbl_schools(school_id,schoolname) values('" + txtSchoolID.Text.Trim() + "',N'" + txtSchoolName.Text.Trim() + "')";
                SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);


                EmptyTextbox();

                s.Init_Schools2(cbSchool1, cbSchool2, cbSchool3, cbSchool4);

            }
            else
            {
                popupEdit.Show();
                strMessage = "Mã trường học đã có.";
            }

        }
        //strMessage = "Tên trường học đã có";
    }
    protected void btnAddNganhHoc_Click(object sender, EventArgs e)
    {
        string sql = string.Empty;

        if (txtTenNganhHoc.Text != "")
        {
            m.MajorSubjectName = txtTenNganhHoc.Text.Trim();
            if (!m.Major_Exist())
            {
                sql = "insert into tbl_majorsubject2(majorsubject_name,active) values(N'" + txtTenNganhHoc.Text.Trim() + "','1')";
                SqlHelper.ExecuteNonQuery(Common.strConnect, CommandType.Text, sql);
                EmptyTextbox();
                m.Init_Major_Subject3(cbMajor1, cbMajor2, cbMajor3, cbMajor4);
            }
            else
            {
                popupNganhHoc.Show();
                strMessage = "Tên ngành học đã có.";
            }
        }

    }

    protected void Export2Excel()
    {
        string filename;
        if (Request.Params["id"] != null)
        {
            string strID = Request.QueryString["id"].ToString();
            Guid id = new Guid(strID);
            Utility ex = new Utility();
            filename = "profile_" + ex.Export2Excel(id) + ".xls";

            string path = "var popup=window.open('../export/" + filename + "');popup.focus();";
            ClientScript.RegisterStartupScript(this.Page.GetType(), "popupOpener", path, true);
          

        }

    }
    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
        Update();
        Export2Excel();
    }
}

